//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ciicos.alarm.service.impl;

import ciicos.alarm.entity.AlarmDto;
import ciicos.alarm.entity.AlarmHisNDataEntity;
import ciicos.alarm.mapper.AlarmHisNDataMapper;
import ciicos.alarm.model.alarmhisndata.AlarmHisNDataForm;
import ciicos.alarm.model.alarmhisndata.AlarmHisNDataPagination;
import ciicos.alarm.service.AlarmHisNDataService;
import ciicos.base.ActionResult;
import ciicos.base.model.filter.RuleInfo;
import ciicos.model.visiual.superQuery.ConditionJsonModel;
import ciicos.model.visiual.superQuery.SuperQueryConditionModel;
import ciicos.model.visiual.superQuery.SuperQueryJsonModel;
import ciicos.permission.model.authorize.AuthorizeConditionModel;
import ciicos.permission.service.AuthorizeService;
import ciicos.permission.util.GenUtil;
import ciicos.util.DateUtil;
import ciicos.util.GeneraterSwapUtil;
import ciicos.util.JsonUtil;
import ciicos.util.ServletUtil;
import ciicos.util.StringUtil;
import ciicos.util.UserProvider;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AlarmHisNDataServiceImpl extends ServiceImpl<AlarmHisNDataMapper, AlarmHisNDataEntity> implements AlarmHisNDataService {
    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;
    @Autowired
    private UserProvider userProvider;
    @Autowired
    private AuthorizeService authorizeService;
    @Autowired
    AlarmHisNDataMapper alarmHisNDataMapper;

    @Override
    public List<AlarmHisNDataEntity> getList(AlarmHisNDataPagination alarmHisNDataPagination) {
        return this.getTypeList(alarmHisNDataPagination, alarmHisNDataPagination.getDataType());
    }

    @Override
    public List<AlarmHisNDataEntity> getTypeList(AlarmHisNDataPagination alarmHisNDataPagination, String dataType) {
        String userId = this.userProvider.get().getUserId();
        List<String> AllIdList = new ArrayList();
        List<List<String>> intersectionList = new ArrayList();
        int total = 0;
        int alarmHisNDataNum = 0;
        QueryWrapper<AlarmHisNDataEntity> alarmHisNDataQueryWrapper = new QueryWrapper();
        List<String> allSuperIDlist = new ArrayList();
        String superOp = "";
        if (ObjectUtil.isNotEmpty(alarmHisNDataPagination.getSuperQueryJson())) {
            SuperQueryJsonModel superQueryJsonModel = (SuperQueryJsonModel)JsonUtil.getJsonToBean(alarmHisNDataPagination.getSuperQueryJson(), SuperQueryJsonModel.class);
            String matchLogic = superQueryJsonModel.getMatchLogic();
            List<ConditionJsonModel> superQueryList = JsonUtil.getJsonToList(superQueryJsonModel.getConditionJson(), ConditionJsonModel.class);

            for(ConditionJsonModel conditionjson : superQueryList) {
                Map<String, Object> map = JsonUtil.stringToMap(conditionjson.getAttr());
                Map<String, Object> configMap = JsonUtil.stringToMap(map.get("__config__").toString());
                String tableName = configMap.get("relationTable") != null ? String.valueOf(configMap.get("relationTable")) : String.valueOf(configMap.get("tableName"));
                if (map.get("multiple") != null && Boolean.valueOf(String.valueOf(map.get("multiple"))) && ObjectUtil.isNull(conditionjson.getFieldValue())) {
                    conditionjson.setFieldValue("[]");
                }

                conditionjson.setTableName(tableName);
            }

            List<String> allSuperList = new ArrayList();
            List<List<String>> intersectionSuperList = new ArrayList();
            int alarmHisNDataNum1 = 0;
            QueryWrapper<AlarmHisNDataEntity> alarmHisNDataSuperWrapper = new QueryWrapper();
            alarmHisNDataNum1 = this.generaterSwapUtil.getCondition(new SuperQueryConditionModel(alarmHisNDataSuperWrapper, superQueryList, matchLogic, "alarm_his_n_data"), new AlarmHisNDataEntity(), alarmHisNDataNum1);
            if (alarmHisNDataNum1 > 0) {
                List<String> alarmHisNDataList = (List)this.list(alarmHisNDataSuperWrapper).stream().map(AlarmHisNDataEntity::getId).collect(Collectors.toList());
                allSuperList.addAll(alarmHisNDataList);
                intersectionSuperList.add(alarmHisNDataList);
            }

            superOp = matchLogic;
            if (matchLogic.equalsIgnoreCase("and")) {
                allSuperIDlist = this.generaterSwapUtil.getIntersection(intersectionSuperList);
            } else {
                allSuperIDlist = allSuperList;
            }
        }

        boolean pcPermission = false;
        boolean appPermission = false;
        boolean isPc = ServletUtil.getHeader("ciicos-origin").equals("pc");
        if (isPc && pcPermission && !this.userProvider.get().getIsAdministrator()) {
            Object alarmHisNDataObj = this.authorizeService.getCondition(new AuthorizeConditionModel(alarmHisNDataQueryWrapper, alarmHisNDataPagination.getMenuId(), "alarm_his_n_data"));
            if (ObjectUtil.isEmpty(alarmHisNDataObj)) {
                return new ArrayList();
            }

            alarmHisNDataQueryWrapper = (QueryWrapper)alarmHisNDataObj;
            if (alarmHisNDataQueryWrapper.getExpression().getNormal().size() > 0) {
                ++alarmHisNDataNum;
            }
        }

        if (!isPc && appPermission && !this.userProvider.get().getIsAdministrator()) {
            Object alarmHisNDataObj = this.authorizeService.getCondition(new AuthorizeConditionModel(alarmHisNDataQueryWrapper, alarmHisNDataPagination.getMenuId(), "alarm_his_n_data"));
            if (ObjectUtil.isEmpty(alarmHisNDataObj)) {
                return new ArrayList();
            }

            alarmHisNDataQueryWrapper = (QueryWrapper)alarmHisNDataObj;
            if (alarmHisNDataQueryWrapper.getExpression().getNormal().size() > 0) {
                ++alarmHisNDataNum;
            }
        }

        if (!this.userProvider.get().getIsAdministrator()) {
            if (alarmHisNDataPagination.getArerList() != null && alarmHisNDataPagination.getArerList().size() > 0) {
                ++alarmHisNDataNum;
                alarmHisNDataQueryWrapper.lambda().in(AlarmHisNDataEntity::getTagarea, alarmHisNDataPagination.getArerList());
            }
        } else if (ObjectUtil.isNotEmpty(alarmHisNDataPagination.getTagarea())) {
            ++alarmHisNDataNum;
            alarmHisNDataQueryWrapper.lambda().like(AlarmHisNDataEntity::getTagarea, alarmHisNDataPagination.getTagarea());
        }

        if (ObjectUtil.isNotEmpty(alarmHisNDataPagination.getTagname())) {
            ++alarmHisNDataNum;
            alarmHisNDataQueryWrapper.lambda().like(AlarmHisNDataEntity::getTagname, alarmHisNDataPagination.getTagname());
        }

        if (ObjectUtil.isNotEmpty(alarmHisNDataPagination.getTagdesc())) {
            ++alarmHisNDataNum;
            alarmHisNDataQueryWrapper.lambda().like(AlarmHisNDataEntity::getTagdesc, alarmHisNDataPagination.getTagdesc());
        }

        if (ObjectUtil.isNotEmpty(alarmHisNDataPagination.getCreatortime_name())) {
            ++alarmHisNDataNum;
            String BelongtimeList = alarmHisNDataPagination.getCreatortime_name();
            Long fir = Long.valueOf(BelongtimeList);
            Long sec = Long.valueOf(BelongtimeList);
            alarmHisNDataQueryWrapper.lambda().ge(AlarmHisNDataEntity::getCreatortime, new Date(fir)).le(AlarmHisNDataEntity::getCreatortime, DateUtil.stringToDate(DateUtil.daFormatYmd(sec) + " 23:59:59"));
        }

        if (ObjectUtil.isNotEmpty(alarmHisNDataPagination.getConfirm()) && alarmHisNDataPagination.getConfirm() != 3) {
            ++alarmHisNDataNum;
            if (alarmHisNDataPagination.getConfirm() == 0) {
                alarmHisNDataQueryWrapper.lambda().in(AlarmHisNDataEntity::getConfirm, new Object[]{0, 4});
            } else {
                alarmHisNDataQueryWrapper.lambda().eq(AlarmHisNDataEntity::getConfirm, alarmHisNDataPagination.getConfirm());
            }
        }

        if (ObjectUtil.isNotEmpty(alarmHisNDataPagination.getAlarmtype())) {
            ++alarmHisNDataNum;
            List<String> idList = new ArrayList();

            try {
                String[][] alarmtype = (String[][])JsonUtil.getJsonToBean(alarmHisNDataPagination.getAlarmtype(), String[][].class);

                for(int i = 0; i < alarmtype.length; ++i) {
                    if (alarmtype[i].length > 0) {
                        idList.add(alarmtype[i][alarmtype[i].length - 1]);
                    }
                }
            } catch (Exception var21) {
                try {
                    List<String> alarmtype = JsonUtil.getJsonToList(alarmHisNDataPagination.getAlarmtype(), String.class);
                    if (alarmtype.size() > 0) {
                        idList.add(alarmtype.get(alarmtype.size() - 1));
                    }
                } catch (Exception var20) {
                    idList.add(String.valueOf(alarmHisNDataPagination.getAlarmtype()));
                }
            }

            alarmHisNDataQueryWrapper.lambda().and((t) -> idList.forEach((tt) -> ((LambdaQueryWrapper)t.like(AlarmHisNDataEntity::getAlarmtype, tt)).or()));
        }

        List<String> intersection = this.generaterSwapUtil.getIntersection(intersectionList);
        if (total > 0) {
            if (intersection.size() == 0) {
                intersection.add("jnpfNullList");
            }

            alarmHisNDataQueryWrapper.lambda().in(AlarmHisNDataEntity::getId, intersection);
        }

        if (StringUtil.isNotEmpty(superOp)) {
            if (allSuperIDlist.size() == 0) {
                allSuperIDlist.add("jnpfNullList");
            }
            List<String> finalAllSuperIDlist = allSuperIDlist;
            alarmHisNDataQueryWrapper.lambda().and(t->t.in(AlarmHisNDataEntity::getId, finalAllSuperIDlist)
            );
        }

        if (StringUtil.isEmpty(alarmHisNDataPagination.getSidx())) {
            alarmHisNDataQueryWrapper.lambda().orderByDesc(AlarmHisNDataEntity::getId);
        } else {
            try {
                String sidx = alarmHisNDataPagination.getSidx();
                AlarmHisNDataEntity alarmHisNDataEntity = new AlarmHisNDataEntity();
                Field declaredField = alarmHisNDataEntity.getClass().getDeclaredField(sidx);
                declaredField.setAccessible(true);
                String value = ((TableField)declaredField.getAnnotation(TableField.class)).value();
                alarmHisNDataQueryWrapper = "asc".equals(alarmHisNDataPagination.getSort().toLowerCase()) ? (QueryWrapper)alarmHisNDataQueryWrapper.orderByAsc(value) : (QueryWrapper)alarmHisNDataQueryWrapper.orderByDesc(value);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }

        if (!"0".equals(dataType)) {
            return this.list(alarmHisNDataQueryWrapper);
        } else if ((total <= 0 || AllIdList.size() <= 0) && total != 0) {
            List<AlarmHisNDataEntity> list = new ArrayList();
            return alarmHisNDataPagination.setData(list, (long)list.size());
        } else {
            Page<AlarmHisNDataEntity> page = new Page(alarmHisNDataPagination.getCurrentPage(), alarmHisNDataPagination.getPageSize());
            IPage<AlarmHisNDataEntity> userIPage = this.page(page, alarmHisNDataQueryWrapper);
            return alarmHisNDataPagination.setData(userIPage.getRecords(), userIPage.getTotal());
        }
    }

    public QueryWrapper wrapperHandle(QueryWrapper<?> wrapper, String id, Class<?> aClass, String type, String tableName) {
        try {
            wrapper.apply(" 1=1 ", new Object[0]);

            for(RuleInfo info : this.generaterSwapUtil.getFilterCondition(id)) {
                String field = info.getField();
                if ((!"main".equals(type) || !field.contains("-")) && (!"main".equals(type) || !field.contains("_jnpf_")) && (!"sub".equals(type) || field.contains("-")) && (!"sub-jnpf".equals(type) || field.contains("_jnpf_"))) {
                    String fieldName = field;
                    String table = "";
                    if (field.contains("-")) {
                        fieldName = field.split("-")[1];
                    }

                    if (field.contains("_jnpf_")) {
                        fieldName = field.split("_jnpf_")[1];
                        table = field.split("_jnpf_")[0];
                        table = table.replace("jnpf_", "");
                    }

                    if (!"sub-jnpf".equals(type) || tableName.equals(table)) {
                        Field declaredField = aClass.getDeclaredField(fieldName);
                        declaredField.setAccessible(true);
                        String fieldDb = ((TableField)declaredField.getAnnotation(TableField.class)).value();
                        GenUtil genUtil = (GenUtil)JsonUtil.getJsonToBean(info, GenUtil.class);
                        genUtil.solveValue(wrapper, fieldDb);
                    }
                }
            }

            return wrapper;
        } catch (Exception var15) {
            return wrapper;
        }
    }

    @Override
    public AlarmHisNDataEntity getInfo(String id) {
        QueryWrapper<AlarmHisNDataEntity> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(AlarmHisNDataEntity::getId, id);
        return (AlarmHisNDataEntity)this.getOne(queryWrapper);
    }

    @Override
    public void create(AlarmHisNDataEntity entity) {
        this.save(entity);
    }

    @Override
    public boolean update(String id, AlarmHisNDataEntity entity) {
        return this.updateById(entity);
    }

    @Override
    public void delete(AlarmHisNDataEntity entity) {
        if (entity != null) {
            this.removeById(entity.getId());
        }

    }

    @Override
    public String checkForm(AlarmHisNDataForm form, int i) {
        int total = 0;
        boolean isUp = StringUtil.isNotEmpty(form.getId()) && !form.getId().equals("0");
        String id = "";
        String countRecover = "";
        if (isUp) {
            id = form.getId();
        }

        return countRecover;
    }

    @Override
    public boolean confirm(AlarmHisNDataEntity entity) {
        String userId = this.userProvider.get().getUserId();
        entity.setLastmodifyuserid(userId);
        entity.setLastmodifytime(new Date());
        return this.updateById(entity);
    }

    @Override
    public ActionResult updateClikcProcess(AlarmDto param) {
        Integer confirm = param.getConfirm();
        String confirmDesc = param.getConfirmDesc();
        if (confirm == null) {
            return ActionResult.fail("参数不全或者修改失败，请检查");
        } else {
            LambdaQueryWrapper<AlarmHisNDataEntity> alarmHisNDataEntityLambdaQueryWrapper = new LambdaQueryWrapper();
            alarmHisNDataEntityLambdaQueryWrapper.eq(AlarmHisNDataEntity::getConfirm, 0);
            List<AlarmHisNDataEntity> alarmHisNDataEntities = this.alarmHisNDataMapper.selectList(alarmHisNDataEntityLambdaQueryWrapper);
            alarmHisNDataEntities.forEach((alarmHisNDataEntity) -> {
                alarmHisNDataEntity.setConfirm(confirm);
                alarmHisNDataEntity.setConfirmdesc(confirmDesc);
                this.confirm(alarmHisNDataEntity);
            });
            return ActionResult.success("更新成功");
        }
    }
}
